﻿'use strict';
(function () {
    var app = angular.module('myApp.Directives', []);
    app.directive('itemContent', ['$compile', 'viewService', function ($compile, viewService) {
        var linker = function (scope, element, attrs) {
            viewService.getTemplates(scope.it.assessmentItemType).then(function (response) {
                var templates = response.data;
                element.html(templates);
                $compile(element.contents())(scope);
            });
        };

        return {
            restrict: 'AEC',
            replace: true,
            link: linker
        };
    }]);

    app.directive('cancelTimer', ['$interval', 'utils', function ($interval, utils) {
        function link($scope, element, attributes) {
            // 当DOM元素从页面中被移除时，AngularJS将会在scope中触发$destory事件。这让我们可以有机会来cancel任何潜在的定时器
            $scope.$on("$destroy", function (event) {
                var timer1 = utils.getData('timer1');
                if (timer1) $interval.cancel(timer1);

                var timer2 = utils.getData('timer2');
                if (timer2) $interval.cancel(timer2);

                var timer3 = utils.getData('timer3');
                if (timer3) $interval.cancel(timer3);
            });
        }

        // 返回指令的配置
        return ({ link: link, scope: false });
    }]);
})();